package cn.hxzy.controller;


import cn.hxzy.pojo.Dis;
import cn.hxzy.service.DisService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author songmengxin
 * @since 2021-07-31
 */
@RestController
@CrossOrigin
public class DisController {
    @Autowired
    private DisService disService;

    @RequestMapping("/insertDis")
    public JSONObject insertDis(@RequestBody Dis dis){
        String uuid= UUID.randomUUID().toString().replaceAll("-","");
        dis.setDisId(uuid);
        dis.setDisTime(new Date());

        boolean save = disService.save(dis);
        JSONObject jsonObject=new JSONObject();
        if(save){
            jsonObject.put("code",0);
            jsonObject.put("msg","增加成功");
        }else{
            jsonObject.put("code",-1);
            jsonObject.put("msg","增加失败");
        }

        return jsonObject;
    }
    @RequestMapping("/getproIdById")
    public JSONObject getproIdById(@RequestParam String proId){
        QueryWrapper<Dis> queryWrapper=new QueryWrapper<>();

        List<Dis> list = disService.getDisById(proId);
        JSONObject jsonObject=new JSONObject();
            jsonObject.put("code",0);
            jsonObject.put("msg","获取数据成功");
            jsonObject.put("count",list.size());
            jsonObject.put("data",list);
        return jsonObject;
    }
    @RequestMapping("/setLikeNumDisById")
    public JSONObject setLikeNumDisById(@RequestParam String disId,@RequestParam Integer dislikeNum){
        Dis dis=new Dis();
        dis.setDislikeNum(dislikeNum);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("dislikeNum",dislikeNum);
        queryWrapper.eq("disId",disId);
        JSONObject jsonObject=new JSONObject();
        boolean update = disService.update(dis, queryWrapper);
        jsonObject.put("code",0);
        jsonObject.put("msg","点赞成功");
        return  jsonObject;
    }

}

